package cn.itcast.goods.book.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.swing.internal.plaf.basic.resources.basic;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.book.service.BookService;
import cn.itcast.goods.pager.PageBean;
import cn.itcast.servlet.BaseServlet;

public class BookServlet extends BaseServlet {
 
	private BookService bookService=new BookService();
	/*
	 * 获取当前页码
	 * */
	private int getPageCode(HttpServletRequest request)
			throws ServletException, IOException {
		String pageCode=request.getParameter("pc");
		if(pageCode==null) return 1;
		
		try {
			return Integer.parseInt(pageCode);
		} catch (RuntimeException e) {
			return 1;	
		}
		
	}
	
	/*
	 * 获取请求的URL，但是去除pc参数
	 * */
	public String getUrl(HttpServletRequest request)
			throws ServletException, IOException {

		String url=request.getRequestURI()+"?"+request.getQueryString();
		int fromIndex=url.lastIndexOf("&pc=");
		if(fromIndex==-1) return url;
		int toIndex=url.indexOf("&",fromIndex+1);
		if(toIndex ==-1) return url.substring(0,fromIndex);
		return url.substring(0, fromIndex)+url.substring(toIndex);
		
	}
	/**
	 * 按分类查询图书（分页）
	 * 
	 * */

	public String findByCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//1.获取当前页码
		int pc=getPageCode(request);
		///2.使用BookService查询，得到PageBean
		String cid=request.getParameter("cid");
		PageBean<Book> pb=bookService.findByCategory(cid, pc);
		//3.获取url，设置给PageBean
		String url=getUrl(request);
		pb.setUrl(url);
		//4.把PageBean保存到request，转发到/jsps/book/list.jsp
		request.setAttribute("pb", pb);
		return "/jsps/book/list.jsp";
		
		
	}
	
	/**
	 * 按作者查询图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByAuthor(HttpServletRequest request, HttpServletResponse response)
	       throws ServletException, IOException {
		
		int pc = getPageCode(request);
		
		String author = request.getParameter("author");
		PageBean<Book> pb=bookService.findByAuthor(author, pc);
		
		String url=getUrl(request);
		pb.setUrl(url);
		
		request.setAttribute("pb",pb);
		return "/jsps/book/list.jsp";
	}
	
	/**
	 * 按出版社查询图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByPress(HttpServletRequest request, HttpServletResponse response)
		       throws ServletException, IOException {
			
			int pc = getPageCode(request);
			
			String press = request.getParameter("press");
			PageBean<Book> pb=bookService.findByPress(press, pc);
			
			String url=getUrl(request);
			pb.setUrl(url);
			
			request.setAttribute("pb",pb);
			return "/jsps/book/list.jsp";
		}
	
	/**
	 * 按书名查询图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByBname(HttpServletRequest request, HttpServletResponse response)
		       throws ServletException, IOException {
			
			int pc = getPageCode(request);
			
			String bname = request.getParameter("bname");
			PageBean<Book> pb=bookService.findByBname(bname, pc);
			
			String url=getUrl(request);
			pb.setUrl(url);
			
			request.setAttribute("pb",pb);
			return "/jsps/book/list.jsp";
		}
	
	/**
	 * 按组合条件查询图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findByCombination(HttpServletRequest request, HttpServletResponse response)
		       throws ServletException, IOException {
			
			int pc = getPageCode(request);
			
			//使用CommonUtils.toBean(map,clazz)获取Book对象
	        Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
			PageBean<Book> pb=bookService.findByCombination(book, pc);
			
			String url=getUrl(request);
			pb.setUrl(url);
			
			request.setAttribute("pb",pb);
			return "/jsps/book/list.jsp";
		}
	/**
	 * 加载图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String bid = request.getParameter("bid");
		request.setAttribute("book", bookService.load(bid));
		
		return "/jsps/book/desc.jsp" ;
	}
	
	
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

}
